package top.wshape1.rpc.message;

import lombok.Data;
import lombok.Getter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Wshape1
 * @version 1.0
 * @description rpc消息抽象类
 * @since 2024-02-01
 */

@Data
public abstract class RpcMessage implements Serializable {

    private int SequenceId;

    private Map<String, Object> attachments;

    private static final Map<Integer, Class<? extends RpcMessage>> msgTypeMap = new HashMap<>();

    public Map<String, Object> getAttachments() {
        if (attachments == null)
            attachments = new HashMap<>();
        return attachments;
    }

    public abstract int getMessageType();

    public static Class<? extends RpcMessage> getMessageType(int msgType) {
        return msgTypeMap.get(msgType);
    }

    static {
        for (MessageType type : MessageType.values()) {
            msgTypeMap.put(type.ordinal(), type.getClazz());
        }
    }

    @Getter
    enum MessageType {
        RPC_REQUEST(RpcRequest.class),
        RPC_RESPONSE(RpcResponse.class),
        PING_PONG(PingPongMessage.class);

        private final Class<? extends RpcMessage> clazz;

        MessageType(Class<? extends RpcMessage> clazz) {
            this.clazz = clazz;
        }

    }

}
